package com.cloud.user_info.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.annex.AnnexUrlService;
import com.cloud.common.domain.Result;
import com.cloud.common.enums.ProjectStatus;
import com.cloud.exception.CustomException;
import com.cloud.system.entity.ViewParam;
import com.cloud.user_info.entity.SysProject;
import com.cloud.user_info.entity.SysProjectRole;
import com.cloud.user_info.mapper.SysProjectMapper;
import com.cloud.user_info.mapper.SysProjectRoleMapper;
import com.cloud.user_info.service.ISysProjectService;
import com.cloud.utils.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统项目配置
 *
 * @author yzj
 * @since 2022-07-25
 */
@Service
public class SysProjectServiceImpl extends ServiceImpl<SysProjectMapper, SysProject> implements ISysProjectService {

    @Resource
    private SysProjectMapper sysProjectMapper;

    @Resource
    private SysProjectRoleMapper projectRoleMapper;

    @Resource
    private AnnexUrlService annexUrlService;

    /**
     * 查询系统页面配置对象
     */
    @Override
    public Result getViewInfo(ViewParam viewParam) {
        SysProject project = sysProjectMapper.getViewByApply(ProjectStatus.APPLIED.getCode(), viewParam.getSysCode());
        if (StringUtils.isNotNull(project)) {
            project.setSysLogo(annexUrlService.getImageOrVideoPath(project.getSysLogo(), viewParam.getLoginType()));
            project.setTopLogo(annexUrlService.getImageOrVideoPath(project.getTopLogo(), viewParam.getLoginType()));
            project.setProjectImage(annexUrlService.getImageOrVideoPath(project.getProjectImage(), viewParam.getLoginType()));
        }
        return Result.success("查询成功", project);
    }

    /**
     * 系统项目配置分页
     */
    @Override
    public List<SysProject> selectSysViewList(SysProject sysProject) {
        return sysProjectMapper.selectSysViewList(sysProject);
    }

    /**
     * 系统项目配置新增
     */
    @Override
    public int insertObject(SysProject sysProject) {
        sysProject.setIsApply(ProjectStatus.NOT_APPLIED.getCode());
        return sysProjectMapper.insert(sysProject);
    }

    /**
     * 系统项目配置修改
     */
    @Override
    public int updateObject(SysProject sysProject) {
        return sysProjectMapper.updateById(sysProject);
    }

    /**
     * 修改配置应用状态
     */
    @Override
    public int turnOnStatus(Long id) {
        return sysProjectMapper.updateApplyStatus(ProjectStatus.APPLIED.getCode(), id);
    }

    /**
     * 关闭配置应用状态
     */
    @Override
    public int closureStatus(Long id) {
        return sysProjectMapper.updateApplyStatus(ProjectStatus.NOT_APPLIED.getCode(), id);
    }

    /**
     * 查询系统项目详情
     */
    @Override
    public SysProject getObjectInfo(Long id) {
        SysProject sysProject = sysProjectMapper.selectById(id);
        if (StringUtils.isNotNull(sysProject)) {
            sysProject.setSysLogo(annexUrlService.getImageTurnPath(sysProject.getSysLogo()));
            sysProject.setTopLogo(annexUrlService.getImageTurnPath(sysProject.getTopLogo()));
            sysProject.setProjectImage(annexUrlService.getImageTurnPath(sysProject.getProjectImage()));
        }
        return sysProject;
    }

    /**
     * 根据用户ID取消用户授权项目
     */
    @Override
    public Result saveProjectRole(String sysCode, List<Long> roleIds) {
        if (StringUtils.isNotEmpty(sysCode)) {
            throw new CustomException("项目编号不能为空");
        }
        if (StringUtils.isNotNull(roleIds)) {
            throw new CustomException("项目角色主键不能为空");
        }
        for (Long roleId : roleIds) {
            SysProjectRole projectRole2 = new SysProjectRole();
            projectRole2.setSysCode(sysCode);
            projectRole2.setRoleId(roleId);
            projectRoleMapper.insert(projectRole2);
        }
        return Result.success("操作成功");
    }

}
